package models

import (
	"time"

	"gorm.io/datatypes"
	"gorm.io/gorm"
)

// MaRobotReport [...] 历史库存信息
type MaRobotInventory struct {
	ID                string         `gorm:"autoIncrement:true;primaryKey;column:id;type:int(10);not null" json:"id" json_old:"ID"`
	RobotName         string         `gorm:"column:robot_name;type:varchar(50);not null;comment:机器名称;uniqueIndex:idx_date_robotName_goodsID,priority:2;index:idx_robotName" json:"robot_name" json_old:"RobotName"`
	Date              datatypes.Date `gorm:"column:date;type:date;not null;comment:日期;uniqueIndex:idx_date_robotName_goodsID,priority:1" json:"-" json_old:"-"`
	DateFormated      string         `gorm:"-" json:"date" json_old:"Date"`
	SalesID           string         `gorm:"column:sales_id;type:varchar(255);not null;default:'';comment:机器识别码" json:"sales_id" json_old:"SalesID"`
	Floor             string         `gorm:"-" json:"floor" json_old:"Floor"`
	ShopID            string         `gorm:"column:shop_id;type:bigint;not null;comment:商户ID" json:"shop_id" json_old:"ShopID"`
	Areas3            string         `gorm:"column:areas3;type:bigint;comment:商场ID" json:"areas3" json_old:"Areas3"`
	GoodsID           int64          `gorm:"column:goods_id;type:bigint;not null;comment:商品ID;uniqueIndex:idx_date_robotName_goodsID,priority:3" json:"goods_id" json_old:"GoodsID"`
	ErrorContainerNum string         `gorm:"column:error_container_num;type:varchar(255);not null;comment:故障货道" json:"error_container_num" json_old:"ErrorContainerNum"`
	GoodsInventory    int64          `gorm:"column:goods_inventory;type:bigint;default:0;comment:截止昨天当前机器商品的库存" json:"goods_inventory" json_old:"GoodsInventory"`
	GoodsMaxInventory int64          `gorm:"column:goods_max_inventory;type:bigint;default:0;comment:截止昨天机器的商品设置的允许最大库存" json:"goods_max_inventory" json_old:"GoodsMaxInventory"`
	ZeroContainerNum  string         `gorm:"column:zero_container_num;type:varchar(255);comment:0库存货道" json:"zero_container_num" json_old:"ZeroContainerNum"`
	CreatedAt         int64          `gorm:"index:created_at;column:created_at;autoCreateTime;type:int;size:64;comment:创建时间" json:"created_at"`
}

// TableName get sql table name.获取数据库表名
func (m MaRobotInventory) TableName() string {
	return "ma_robot_inventory"
}

func (m *MaRobotInventory) AfterFind(tx *gorm.DB) (err error) {
	if !time.Time(m.Date).IsZero() {
		DateByte, _ := DateTime(m.Date).MarshalDate()
		m.DateFormated = string(DateByte)
	}
	return
}

// func (m *MaAd) AfterFind(tx *gorm.DB) (err error) {
// 	if !time.Time(m.EndTime).IsZero() {
// 		EndTimeByte, _ := DateTime(m.EndTime).MarshalDate()
// 		m.EndTimeFormated = string(EndTimeByte)
// 	}
// 	return
// }

// 模式自动注册
func init() {
	Models = append(Models, &MaRobotInventory{})
}
